<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<script src="../js/pages/public.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="del()"><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
									<form id="selectForm1">
										<input name="specName" class="form-control" placeholder="规格名称">
										<input type="button" value="搜索" onclick="load()">
									</form>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="spec">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<a href="javascript:getpage(1)">首页</a>
							<a href="javascript:getpage(2)">上一页</a>
							<a>当前第<input name="pageNum" size="1" id="pageNum" value="1">页
								<a>每页显示<input id="pageSize" size="1">条记录共<input size="1" id="allCount">条记录</a>
								<a href="javascript:getpage(3)">下一页</a>
								<a href="javascript:getpage(4)">末页</a>
							</a>
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->


  <!-- 修改窗口 -->
<from id="updatefrom">
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">规格编辑</h3>
			  </div>
			  <div class="modal-body">
				  <table class="table table-bordered table-striped"  width="800px">
					  <input type="text" name="id" id="specId">
					  <tr>
						  <td>规格名称</td>
						  <td><input id="updateSpecName" name="specName" class="form-control" placeholder="规格名称" >  </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="新建" ><i class="fa fa-file-o"></i> 新增规格选项</button>

				  </div>

				  <table class="table table-bordered table-striped table-hover dataTable">
					  <thead>
					  <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
					  </thead>
					  <tbody id="updateTbody">

					  </tbody>
				  </table>


			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updatebrand()">修改</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</from>


  <!-- 增加窗口 -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h3 id="myModalLabel">规格编辑</h3>
			</div>
			<div class="modal-body">

				<table class="table table-bordered table-striped" width="800px" id="specOption">
					<tr>
						<td>规格名称</td>
						<td><input class="form-control" placeholder="规格名称" name="specName" id="specName1"></td>
					</tr>
				</table>

				<!-- 规格选项 -->
				<div class="btn-group">
					<button type="button" class="btn btn-default" title="新建" id="insertTr"><i class="fa fa-file-o"></i>
						新增规格选项
					</button>

				</div>

				<table class="table table-bordered table-striped table-hover dataTable" id="table">
					<thead>
					<tr>
						<th class="sorting">规格选项</th>
						<th class="sorting">排序</th>
						<th class="sorting">操作</th>
					</tr>
					</thead>
					<tbody>
					<tr>

						<td>
							<input class="form-control" placeholder="规格选项">
						</td>
						<td>
							<input class="form-control" placeholder="排序">
						</td>
						<td>
							<button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
						</td>
					</tr>
					<tr>

						<td>
							<input class="form-control" placeholder="规格选项">
						</td>
						<td>
							<input class="form-control" placeholder="排序">
						</td>
						<td>
							<button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
						</td>
					</tr>
					<tr>

						<td>
							<input class="form-control" placeholder="规格选项">
						</td>
						<td>
							<input class="form-control" placeholder="排序">
						</td>
						<td>
							<button type="button" class="btnDelete" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
						</td>
					</tr>
					</tbody>
				</table>


			</div>
			<div class="modal-footer">
				<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
				<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			</div>
		</div>
	</div>
</div>

</body>

</html>
<script>
	var token = window.localStorage.getItem("token");

	$(function() {
		select();
		getpage(1);
	})

	function load() {
		select();
	}

	function getpage(id) {
		var dang = parseInt($("#pageNum").val());
		var gong = parseInt($("#allCount").val());
		var zong = parseInt($("#pageSize").val());
		var o =gong-(gong%zong==0? gong/zong : gong%zong);
		var mo = (o/zong)+1;
		if (id==1){
			$("#pageNum").val(1);
		}else if (id==2){
			$("#pageNum").val(dang-1);
		}else if (id==3){
			$("#pageNum").val(dang+1);
		}else if (id==4){
			$("#pageNum").val(mo);
		}
		select()
	}

	function select() {
		//获取条件查询的值
		var parms = $("#selectForm1").serializeObject();
		var page = {"pageNum":$("#pageNum").val(),"pageSize":5,"parms":parms};
		$.ajax({
			url: domain+"/spec/findSpecPage",
			headers : {"AutharizeToken":token},
			contentType: 'application/json;charset=utf-8',
			type:"post",
			dataType:"json",
			//将传到后台的数据转为json格式
			data: JSON.stringify(page),
			//异步请求
			async:false,
			success:function (result) {
				if (result.code==10000){
					var list = result.data.rows;
					//拼接路径
					var  html = '';
					$(list).each(function(i,e) {
						html += '<tr>'
						html += '<td><input name="checkbox" value="'+e.id+'" type="checkbox" ></td>'
						html += '<td>'+ e.id+'</td>'
						html += '<td>'+ e.specName+'</td>'
						html += '<td className="text-center" align="center">'
						html += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="toupdatebrand('+e.id+')" >修改</button> '
						html += '</td>'
						html += '</tr>'
					})
					$("#spec").html(html)
					$("#pageSize").val(result.data.pageSize);
					$("#pageNum").val(result.data.pageNum);
					$("#allCount").val(result.data.total);
				}
			},error:function () {
				alert("系统异常")
			}
		})
	}

	function del() {
		var ids = [];
		var checkbox=$("input[name=checkbox]");
		$(checkbox).each(function (i,v) {
			if ($(v).prop("checked")){
				ids.push($(v).val())
			}
		})
		if (ids.length <= 0) {
			alert("请选择要删除的id");
			return
		}
		if (confirm("是否确定要删除")) {
			$.ajax({
				url: domain + "/spec/delete?ids=" + ids,
				headers : {"AutharizeToken":token},
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				type: "post",
				async: false,
				success: function (result) {
					location.reload();
				}, error: function () {
					alert("系统错误")
				}
			})
		}
	}

	//去修改
	function toupdatebrand(id) {
		$.ajax({
			headers : {"AutharizeToken":token},
			url:domain+"/spec/getSpecById",
			type : "POST",
			data:{id:id},
			dataType : "json",
			async : false,
			success : function(result) {
				if (result.code==10000){
					var specvo = result.data;
					console.log(specvo)
					//拼接
					$("#specId").val(result.data.id);
					$("#updateSpecName").val(specvo.specName);
					var html=''
					$(specvo.list).each(function (i,e) {
						html += '<tr>'
						html += '<td>'
						html += '<input  class="form-control" name="optionName" value="'+e.optionName+'" placeholder="规格选项">'
						html += '</td>'
						html += '<td>'
						html += '<input  class="form-control" name="orders" value="'+e.orders+'" placeholder="排序">'
						html += '</td>'
						html += '<td>'
						html += '<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>'
						html += '</td>'
						html += '</tr>'
						$("#updateTbody").html(html)
					})
				}
			},error : function() {
				alert("系统错误")
			}
		})
	}

	function updatebrand() {
		var spec = {};//想当于是map集合 .put(key,value)
		spec["id"] = $("#specId").val();
		spec["specName"] = $("#updateSpecName").val();
		var arr = [];
		var trList = $("#updateTbody").find("tr");//查找tr的行
		$(trList).each(function (i, v) {
			var tdList = $(v).find("td");//此时
			var option = {"optionName": $(tdList[0]).find("input").val(), "orders": $(tdList[1]).find("input").val()}
			arr.push(option);
		})
		spec["list"] = arr;
		$.ajax({
			headers : {"AutharizeToken":token},
			url: domain + "/spec/updateSpec",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			type: "post",
			async: false,
			data: JSON.stringify(spec),
			success: function (result) {
				if (result.code == 10000) {
					alert("修改成功")
					location.reload();
				}
			}, error: function () {
				alert("系统错误")
			}
		})
	}

	//增加
	function insert() {
		var optionList = [];
		var trList = $("#table").find("tr")
		$(trList).each(function (i, v) {
			var tdList = $(v).find("td");
			var option = {"optionName": $(tdList[0]).find("input").val(), "orders": $(tdList[1]).find("input").val()}
			optionList.push(option);
		})
		var spec = {}
		spec["specName"] = $("#specName1").val();
		spec["list"] = optionList;
		$.ajax({
			headers : {"AutharizeToken":token},
			url: domain + "/spec/addSpec",
			contentType: "application/json;charset=utf-8",//传过去的格式为json格式
			type: "post",
			dataType: "json",
			data: JSON.stringify(spec),
			success: function (result) {
				alert("增加成功")
				window.location.reload();
			},
			error: function (result) {
				alert("错误");
			}
		})
	}
	//表格尾部增加一行
	$(function () {
		$("#insertTr").click(function () {
			$("#table tbody").append('<tr><td><input  class="form-contr+ol" placeholder="规格选项"></td><td><input  class="form-control" placeholder="排序"></td><td><button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td></tr>');
		});
	})

	//表格删除任意一行
	$(function () {
		$("#table").on('click', '.btnDelete', function () {
			$(this).closest('tr').remove();
		});
	})

	//表格尾部增加一行
	$(function () {
		$("#insertTr1").click(function () {
			$("#table1 tbody").append('<tr><td><input  class="form-control" placeholder="规格选项"></td><td><input  class="form-control" placeholder="排序"></td><td><button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td></tr>');
		});
	})

	//表格删除任意一行
	$(function () {
		$("#table1").on('click', '.btnDelete', function () {
			$(this).closest('tr').remove();
		});
	})

</script>